package com.zc.app.shiro.controller;

import com.zc.app.shiro.peer.ResourcePeer;
import com.zc.app.shiro.service.ResourceService;
import com.zc.app.shiro.service.UserService;
import com.zc.app.shiro.utils.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.apache.xpath.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
 * Created by zc on 2016/7/22.
 */
//@Controller
public class LoginController {
    private static Logger logger = LogManager.getLogger(LoginController.class);

    @RequestMapping(value = "/login")
    public String showLoginForm(HttpServletRequest req, Model model){
        String exceptionClassName = (String) req.getAttribute("shiroLoginFailure");
        String error = null;
        if(UnknownAccountException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if(IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if(exceptionClassName != null) {
            error = "其他错误：" + exceptionClassName;
        }
        model.addAttribute("error", error);

        if(SecurityUtils.getSubject().isAuthenticated()){
            return "forward:/main";
        }

        return "login";
    }

//    @RequestMapping(value = "/logout.action")
//    public String logout(HttpServletRequest req, Model model) {
//        Subject subject = SecurityUtils.getSubject();
//        if (subject != null && subject.isAuthenticated()) {
//            subject.logout();
//        }
//        return "login";
//    }
}
